<?php
namespace app\api\controller;

use app\common\controller\Api;
use fds\FdsSdk;

/**
 * 支付demo
 */
class Pay extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    /**
     * 初始化函数
     *
     */
    public function _initialize() 
    {
        date_default_timezone_set('PRC');           // 设置时区

        // 应用编号，需修改
        FdsSdk::setAppId("7348984221107961857");
   
        FdsSdk::setServerUrl("https://moneyconnect.lakala.com/service/soa");
        FdsSdk::setApiVersion("3.0");

        $certPath               = EXTEND_PATH .'fds'. DS .'cert'. DS;

        $DSAPublicKeyPath       = $certPath  .'cloud_dsa_public.pem';                       // 钱帐通的DSA公钥文件所在目录，需修改
        $DSAPrivateKeyPath      = $certPath  .'dsa_private_key.pfx';                        // 应用自己的DSA私钥文件所在目录，需修改
        $DSAPrivateKeyPass      = 'atcrahkij7veqdja';                                       // 应用自己的DSA私钥密码，需修改
        FdsSdk::setDSAKey($DSAPublicKeyPath, $DSAPrivateKeyPath, $DSAPrivateKeyPass);

        $RSAPublicKeyPath       = $certPath  .'cloud_rsa_public.pem';                       // 钱帐通的RSA公钥文件所在目录，需修改
        $RSAPrivateKeyPath      = $certPath  .'rsa_private_key.pfx';                        // 应用自己的RSA私钥文件所在目录，需修改
        $RSAPrivateKeyPass      = '2t1fyyq47ekq6ne4';                                       // 应用自己的RSA私钥密码，需修改
        FdsSdk::setRSAKey($RSAPublicKeyPath, $RSAPrivateKeyPath, $RSAPrivateKeyPass);
    }

    public function test()
    {
        return json_encode(['code'=>1,'msg'=>'success','data'=>['user'=>1111]]);
    }

    /**
     * 1.3消费接口
     *
     */
    public function consume()
    {
        $params = $this->request->param();
        ///// 组装请求参数
        // $data                   = [
        //     'seller_member_no'      => 6806043394058620928,                         // 卖家标识
        //     'out_order_no'          => 'no'. $params['order_no'],                   // 应用平台订单号（唯一的）
        //     'amount'                => $params['fee'],                              // 订单金额（单位：分）
        //     'pay_amount'            => $params['fee'],                              // 首次支付金额（单位：分）
        //     // 'terminal_ip'           => '113.104.180.3',                          // 终端IP
        //     'terminal_ip'           => $this->request->ip(),                        // 终端IP
        //     'order_name'            => '测试支付',                                   // 订单名称
        //     'split_mode'            => 0,                                           // 分账模式
        //     /* 'front_url'             => '',                                       // 前台通知地址
        //     'back_url'              => '',                                          // 后台通知地址 */
        // ];

        // ////// 支付方式
        // $data['pay_method']     = ['GATEWAY' => [
        //     'member_no'             => 6806043394058620928,                         // 卖家标识
        //     'merchant_no'           => 7194643510113705984,                         // 钱帐通商户号                                     
        //     'amount'                => $params['fee'],                              // 支付金额，单位：分
        //     'counter_param'         => ['pay_mode' => $params['pay_mode']],    
        //     // 'counter_param'         => ['pay_mode' => 'ALIPAY'],    
        //     // 'counter_param'         => ['pay_mode' => 'WECHAT'],    
        //     'product_name'          => '测试商品',
        //     'inte_routing'          => 1,
        // ]];

        // ///// 分账规则
        // $data['split_rule_data']= ['split_list' => [
        //     ['member_no' => 6806043394058620928, 'amount' => $params['fee'], 'special_account_no' => 'S005'],    // S005分到商户佣金专用账户，仅商户会员
        //     ['member_no' => 6806043394058620928, 'amount' => $params['fee'],    // S005分到商户佣金专用账户，仅商户会员
        // ]];

        $result                 = FdsSdk::getService()->createCom($params, 'order.consume.request');
        $this->success('', $result);
        // dump($result); exit();

        // //// 返回成功跳转
        // if (isset($result['status']) && $result['status'] == 'OK') {
        //     header("Location: ". $result['result']['pay_info']['counter_url']);
        // } else {
        //     $this->error($result);
        // }
    }


    /**
     * 4.4 消费支付结果通知
     *
     */
    public function check_consume()
    {
        ///// 组装请求参数
        $params = $this->request->param();
        $data = [];
        $data['order_no'] = $params['order_no'];
        $data['out_order_no'] = 'no'. $params['order_no'];
        $data['seller_member_no'] = 6806043394058620928;
        $data['amount'] = $params['fee'];
        $data['pay_amount'] = $params['fee'];
        $data['order_name'] = '测试商品';
        $data['order_desc'] = '测试商品';
        // $data['order_status'] = [];
        // $data['pay_status'] = [];
        // $data['pay_seq_no'] = [];
        $data['pay_time'] = date("Y-m-d H:i:s");
        // $data['status'] = [];
        $result = FdsSdk::getService()->createCom($data, 'order.consume.pay');
        // dump($result); exit();

        //// 返回成功跳转
//        if (isset($result['status']) && $result['status'] == 'OK') {
//            header("Location: " . $result['result']['pay_info']['counter_url']);
//        } else {
//            $this->error($result);
//        }
        $this->success('', $result);
    }

    /**
     * 1.15分账接口
     *
     */
    public function split()
    {
        ///// 组装请求参数
        // $data                   = [
        //     'member_no'             => 6806043394058620928,                         // 卖家标识
        //     'account_type'          => '1000',                                      // 账户类型编码
        //     'out_order_no'          => '7272915734750400512',                       // 订单号
        //     'amount'                => 1,                                           // 订单金额（单位：分）
        // ];

        // ///// 分账规则
        // $data['split_rule_data']= ['split_list' => [
        //     ['member_no' => 6806043394058620928, 'amount' => 1],
        // ]];

         $data = $this->request->param();

        $result                 = FdsSdk::getService()->createCom($data, 'order.balance.split');
        $this->success('', $result);
    }
    
    /**
     * 1.6 退款
     *
     */
    public function refund()
    {
        $data = $this->request->param();
        ///// 组装请求参数
        // $data                   = [
        //     'order_no'              => 7272915734750400512,                 // 原订单（需要退款的订单编号）
        //     'out_request_no'        => '24121211012001101011001705598',     // 应用平台请求号（对应消费下单时返回的字段pay_order_no）
        //     'pay_seq_no'            => '7272915734872035328',               // 支付流水号
        //     'refund_amount'         => 10,                                  // 退款金额                                
        // ];

        // $data['refund_rule']    = [[                                        // 退款规则
        //     'member_no'             => 6806043394058620928,                                  
        //     'account_type_no'       => '1000',                                  
        //     'amount'                => 10,                                  
        // ]];

        $result                 = FdsSdk::getService()->createCom($data, 'order.consume.refund');
        $this->success('', $result);
    }


    /**
     * 1.2 提现
     *
     */
    public function withdraw()
    {
        ///// 组装请求参数
        $data                   = [
            'member_no'             => 6806043394058620928,                         // 卖家标识       
            'out_order_no'          => 'ww'. date('YmdHi'),                         // 应用平台订单号                         
            'account_type'          => '1000',                                      
            'amount'                => 10,                                          // 提现金额（含平台手续费） 单位：分        
            'billing_fee'           => 1,                                           // 	平台手续费      单位：分
            'bank_card_id'          => '6806043785454292992',                       // 银行卡的编号   
            'withdraw_type'         => 'D0',                                        // 提现方式        
            'back_url'              => 'http://fish.fa15.com/',                     // 后台通知地址                           
        ];

        $result = FdsSdk::getService()->createCom($data, 'order.withdraw.request');
        $this->success('', $result);
    }


    /**
     * 2.1开户进阶接口
     *
     */
    public function addUser()
    {
        /**
         * out_member_no        外部会员标识——必选
         * open_part            开通角色——必选
         * back_url             后台通知地址——必选
         * member_category      会员类别——必选
         * member_role          会员角色——必选
         * member_type          会员类型——必选
         * */
        ///// 组装请求参数
        $params = $this->request->param();

        $result   = FdsSdk::getService()->createCom($params, 'member.open.part.page.url.get');
        $this->success('', $result);
    }


    /**
     * 2.3 会员绑卡相关页面（H5-URL）
     *
     */
    public function bankCard()
    {
        /**
         * member_no            应用平台会员标识
         * back_url             后台通知地址
         * */
        ///// 组装请求参数
        $params = $this->request->param();
        $result   = FdsSdk::getService()->createCom($params, 'member.bank.card.page.url.get');
        $this->success('', $result);
    }

    /**
     * 2.5 查询会员信息
     *
     */
    public function findUser()
    {
        $result                 = FdsSdk::getService()->createCom(['member_no' => 6806043394058620928], 'member.info.query');
        $this->success('', $result);
    }


    /**
     * 2.7 查询绑定银行卡列表
     * member_no     会员标识
     */
    public function findBank()
    {
        ///// 组装请求参数
        $params = $this->request->param();
        $result                 = FdsSdk::getService()->createCom($params, 'member.bank.card.bind.query');
        $this->success('', $result);
    }


    /**
     * 3.1 查询余额
     *
     */
    public function findBalance()
    {
        $params = $this->request->param();
        // $result                 = FdsSdk::getService()->createCom(['member_no' => 6633965347487748096, 'account_type' => '1000'], 'account.balance.get');
        $result                 = FdsSdk::getService()->createCom($params, 'account.balance.get');
        $this->success('', $result);
    }

    /**
     * 3.2 托管账户查询余额
     *
     */
    public function findTrustBalance()
    {
        $params = $this->request->param();
        // $result                 = FdsSdk::getService()->createCom(['member_no' => 6633965347487748096, 'account_type' => '1000'], 'account.balance.get');
        $result                 = FdsSdk::getService()->createCom($params, 'account.trust.balance.get');
        $this->success('', $result);
    }

    public function demo()
    {
        // rsa加密方法
        // FdsSdk::getService()->rsaEncrypt("332624199001011111");

        //外部会员标识，需修改
        $result         = FdsSdk::getService()->createCom(['out_member_no' => 'member0001'], 'member.person.create');
        $this->success('', $result);
    }

}
